import { getSecond, uuid } from '../../utils/common'
import { _sendVerificationCode , _checkVerificationCode } from '../../utils/sendCode'
import { ManageAdmin  } from '../../models/'
import { CODE , SEND_TIME , TOKEN_TIME , VERIFICATION_CODE_TYPE , UID_KEY } from '../../config/config'
import { _getAccount , _bindPhone } from '../../utils/account' 

/**
 * @api {post} /manage_admin/login 管理员登录
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName login
 * @apiGroup z_manage_admin
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/account/login'

 * @apiParam {String}  username                 用户名
 * @apiParam {String}  password                 密码
 *
 *
 * @apiSuccess {Object}   admin                管理员信息
 * 
 * @apiSuccess {String}   uid                  管理员uid/token
 * @apiSuccess {String}   level                管理员等级
 * @apiSuccess {String}   nickname             管理员昵称
 * @apiSuccess {String}   username             管理员用户名
 * @apiSuccess {String}   remark               管理员备注
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_admin/login
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function login(ctx) {
    const username  	   = ctx.request.body.username
    const password  	   = ctx.request.body.password

	const manageAdmin      = await ManageAdmin.findOne({where : {username,password,status:1}})
	if(manageAdmin) {
		const admin        = {
			uid 	 : manageAdmin.uid,
			level 	 : manageAdmin.level,
			nickname : manageAdmin.nickname,
			username : manageAdmin.username,
			remark 	 : manageAdmin.remark,
			ext 	 : manageAdmin.ext,
			ctime 	 : manageAdmin.ctime
		}
		ctx.body            = CODE.success({admin})
		return
	}
	ctx.body                = CODE.MANAGE_ERROR.ERROR_USERNAME_PASSWORD_NOT_CHECK

}

/**
 * @api {post} /manage_admin/createAdmin 创建管理员
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName createAdmin
 * @apiGroup z_manage_admin
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_admin/createAdmin'

 * @apiParam {String}   token             	 操作者用户token

 * @apiParam {String}   username             管理员用户名
 * @apiParam {String}   password             管理员密码
 * @apiParam {String}   level                管理员等级
 * @apiParam {String}   nickname             管理员昵称
 * @apiParam {String}   remark               管理员备注
 * @apiParam {String}   ext                  拓展字段
 *
 * @apiSuccess {Object}   admin                管理员信息
 * @apiSuccess {String}   uid                  管理员uid/token
 * @apiSuccess {String}   level                管理员等级
 * @apiSuccess {String}   nickname             管理员昵称
 * @apiSuccess {String}   username             管理员用户名
 * @apiSuccess {String}   remark               管理员备注
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_admin/createAdmin
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function createAdmin(ctx) {
	const admin             = ctx.body.admin

	const level				= ctx.request.body.level
	const username			= ctx.request.body.username
	const password			= ctx.request.body.password
	const nickname			= ctx.request.body.nickname
	const ext				= ctx.request.body.ext
	const remark			= ctx.request.body.remark
	const ctime 			= getSecond()

	if(Number(level) >= Number(admin.level)) {
		ctx.body            = CODE.MANAGE_ERROR.ERROR_LEVEL
		return
	}
	const uid               = uuid()
	const manageAdmin       = await ManageAdmin.create({uid,level,username,password,nickname,ext,remark,ctime})
	if(manageAdmin) {
		ctx.body            = CODE.success({
			admin:{
				uid 	 : manageAdmin.uid,
				level 	 : manageAdmin.level,
				nickname : manageAdmin.nickname,
				username : manageAdmin.username,
				remark 	 : manageAdmin.remark,
				ext 	 : manageAdmin.ext,
				ctime 	 : manageAdmin.ctime
			}
		})
		return
	}
	ctx.body            = CODE.MANAGE_ERROR.ERROR_ADMIN_CREATE
}


/**
 * @api {post} /manage_admin/updateAdmin 修改管理员信息
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName updateAdmin
 * @apiGroup z_manage_admin
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_admin/updateAdmin'

 * @apiParam {String}   token             	 操作者用户token

 * @apiParam {String}   uid                  管理员uid/toke 
 * @apiParam {String}   password             管理员密码
 * @apiParam {String}   level                管理员等级
 * @apiParam {String}   nickname             管理员昵称
 * @apiParam {String}   remark               管理员备注
 * @apiParam {String}   ext                  拓展字段
 *
 * @apiSuccess {Object}   admin                管理员信息
 * @apiSuccess {String}   uid                  管理员uid/token
 * @apiSuccess {String}   level                管理员等级
 * @apiSuccess {String}   nickname             管理员昵称
 * @apiSuccess {String}   username             管理员用户名
 * @apiSuccess {String}   remark               管理员备注
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_admin/updateAdmin
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function updateAdmin(ctx) {
	const admin             = ctx.body.admin
	const uid				= ctx.request.body.uid

	const level				= ctx.request.body.level
	const password			= ctx.request.body.password
	const nickname			= ctx.request.body.nickname
	const ext				= ctx.request.body.ext
	const remark			= ctx.request.body.remark

	const manageAdmin       = await ManageAdmin.findOne({where : {uid,status:1}})
	if(!manageAdmin) {
		ctx.body            = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
		return
	}

	if(Number(manageAdmin.level) >= Number(admin.level) && manageAdmin.uid != admin.uid) {
		ctx.body            = CODE.MANAGE_ERROR.ERROR_LEVEL
		return
	}
	manageAdmin.level       = level || manageAdmin.level
	manageAdmin.password    = password || manageAdmin.password
	manageAdmin.nickname    = nickname || manageAdmin.nickname
	manageAdmin.ext         = ext || manageAdmin.ext
	manageAdmin.remark      = remark || manageAdmin.remark
	await manageAdmin.save()
	ctx.body                = CODE.success({
		admin:{
				uid 	 : manageAdmin.uid,
				level 	 : manageAdmin.level,
				nickname : manageAdmin.nickname,
				username : manageAdmin.username,
				remark 	 : manageAdmin.remark,
				ext 	 : manageAdmin.ext,
				ctime 	 : manageAdmin.ctime
			}
	})
}


/**
 * @api {post} /manage_admin/deleteAdmin 删除管理员
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName deleteAdmin
 * @apiGroup z_manage_admin
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_admin/deleteAdmin'

 * @apiParam {String}   token             	 操作者用户token

 * @apiParam {String}   uid                  管理员uid/token
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_admin/deleteAdmin
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function deleteAdmin(ctx) {
	const admin             = ctx.body.admin
	const uid				= ctx.request.body.uid

	const manageAdmin       = await ManageAdmin.findOne({where : {uid,status:1}})
	if(!manageAdmin) {
		ctx.body            = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
		return
	}
	if(Number(manageAdmin.level) >= Number(admin.level)) {
		ctx.body            = CODE.MANAGE_ERROR.ERROR_LEVEL
		return
	}
	manageAdmin.status      = 2
	await manageAdmin.save()
	ctx.body                = CODE.success()
}

export async function checkToken(ctx,next) {
	const token  = ctx.request.body.token
	const admin  = await ManageAdmin.findOne({where:{uid:token,status:1}})
	if(admin) {
		ctx.body = { admin }
		if(next) {
			return next()
		}
	}
	ctx.body     = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
}
/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */